﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
//using U8Login;
using System.Windows.Forms;
//using UFIDA.U8.Cust.Public;
using System.Data;
using System.Data.OleDb;
//using UFIDA.U8.Cust.Public.Eorr;
using System.Data.Common;
using System.Data.SqlClient;
//using UFIDA.U8.Cust.Public.UserInfor;

using ADODB;
using System.Xml;

namespace PublishSys.Public
{
    public class Common 
    {

        private static string URL = Common.getConfigInfo("ncurl");
        //读取App.config的值
        public static string getConfigInfo(string key)
        {
            string value = "";
            value = ConfigurationManager.AppSettings[key];
            return value;
        }

        //设置App.config的值
        public static bool setConfigInfo(string key, string value)
        {
            try {
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration.AppSettings.Settings[key].Value = value;
                configuration.Save();
                
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }            
            catch {
                return false;
            }
        }

        #region 根据DataTable更新表
        /// <summary>
        /// 为指定 DataTable 中每个已插入、已更新或已删除的行调用相应的 INSERT、UPDATE 或 DELETE 语句
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static bool UpdateByDataTable(DataTable dt,string sql)
        {

            //user.dbConnecttion.Open();
            try
            {
                //OleDbDataAdapter sda = new OleDbDataAdapter(sql, user.dbConnecttion);

                //OleDbCommandBuilder SCB = new OleDbCommandBuilder(sda);
                //sda.Update(dt);
                return true;
            }
          
            catch (Exception e) {
                //LogHelper.WriteLog(typeof(Common), e.Message);
                return false;
            }
            finally
            {
                //user.dbConnecttion.Close();
            }
            
        }

        #endregion



        #region 根据sql 返回DataTable

        /// <summary>
        /// 根据sql取数 返回datatable
        /// </summary>
        /// <param name="strsql"></param>
        /// <param name="conn"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static DataTable getDataTableBySql(string strsql, string conn) {

            using (SqlConnection myCn = new SqlConnection(conn))
            {
                using (SqlCommand cmd = new SqlCommand(strsql, myCn))
                {                    
                    try
                    {
                        myCn.Open();
                        cmd.CommandTimeout = 1000;
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        return dt;
                    }
                    catch (SqlException se)
                    {
                        throw new Exception(se.Message);
                    }
                    finally
                    {
                        myCn.Close();
                    }
                }
            }
        }

        #endregion



        /// <summary>
        /// 根据connection 执行sql
        /// </summary>
        /// <param name="sql">sql脚本</param>
        /// <param name="connstring">连接串</param>
        private static void UpdateBySql(string sql, string connstring)
        {
            using (SqlConnection conn = new SqlConnection(connstring))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (SqlException se)
                    {
                        throw new Exception(se.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
        public static string getconntionstring()
        {
            string server = Common.getConfigInfo("server");
            string db = Common.getConfigInfo("db");
            string user = Common.getConfigInfo("user");
            string password = Common.getConfigInfo("password");
            string connectionString = "Server = " + server + ";User = " + user + "; Pwd = " + password + ";Database = " + db;
            return connectionString;
        }

        
    }
}
